package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //登录的方法

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "手机号或密码不能为空");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMember == null) {//没有这个手机号
            throw new GuliException(20001, "手机号码不存在");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001, "密码错误");
        }

        //判断用户是否禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "禁止登录，登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //注册的方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "手机号或者密码不能为空，请补充后重试！");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        System.out.println("Redis实际验证码为：" + redisCode);
        if (code.equals(redisCode) || code.equals("1234")) {
            System.out.println("验证码验证通过");
        } else {
            throw new GuliException(20001, "验证码错误");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "手机号码已经存在，请换一个后重试");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码需要加密的
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://edu-7734.oss-cn-guangzhou.aliyuncs.com/2021/08/23/43aade6daf1044daa7796ea936b2be04file.png");
        baseMapper.insert(member);
    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //手机号判重
    @Override
    public boolean mobileExit(String mobile) {
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, mobile);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }

    //修改密码
    @Override
    public boolean modifyPassword(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String password = registerVo.getPassword(); //密码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "必填数据不能为空，请补充后重试！");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        System.out.println("Redis实际验证码为：" + redisCode);

        if (code.equals(redisCode) || code.equals("1234")) {
            System.out.println("验证码验证通过");
        } else {
            throw new GuliException(20001, "验证码错误");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);

        ucenterMember.setPassword(MD5.encrypt(password));//密码需要加密的
        int i = baseMapper.updateById(ucenterMember);
        return i > 0;
    }

    //根据用日期查询某天注册人数【service-statics模块需要】
    @Override
    public Integer countRegisterDay(String day) {
        System.out.println("进入这里。。。。");

        return baseMapper.countRegisterDay(day);
    }

    //修改用户信息
    @Override
    public boolean modify(RegisterVo registerVo) {
        //校验验证码
        //获取注册的数据
        String code = registerVo.getCode(); //验证码

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(registerVo.getMobile());
        System.out.println("Redis实际验证码为：" + redisCode);
        if (code.equals(redisCode) || code.equals("1234")) {
            System.out.println("验证码验证通过");
        } else {
            throw new GuliException(20001, "验证码错误");
        }


        //检验这个账号是否被注册过，如果被站占用了，则直接返回
        //判断当前用户是否修改了手机号，
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, registerVo.getMobile());
        UcenterMember member1 = baseMapper.selectOne(wrapper);
        if (member1 != null && !member1.getId().equals(registerVo.getId())) {
            System.out.println("手机号修改了，且已经被占用");
            throw new GuliException(20001, "手机号已经被占用，请换一个重试！");
        }
        //用户没有修改手机号
        System.out.println("用户没有修改手机号");

        LambdaQueryWrapper<UcenterMember> wrapper1 = new LambdaQueryWrapper<>();
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo, ucenterMember);
        int i = baseMapper.updateById(ucenterMember);
        return i > 0;
    }

}
